package com.imti.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imti.common.PageBean;
import com.imti.common.Result;
import com.imti.pojo.User;
import com.imti.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping
    public Result<PageBean<User>> findAllUser(@RequestParam(value = "userName", required = false) String userName,
                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){

        IPage<User> page = new Page<>(pageNum, pageSize);

        return Result.success(PageBean.restPage(userService.lambdaQuery().like(ObjectUtil.isNotNull(userName) && StringUtils.isNotBlank(userName), User::getUsername, userName).page(page)));
    }
}
